<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    ***************************************************************************
    Updated: 06/05/2009
    
    TODO: 
    1. Since MonitoredDataItem could not appear if IsContextType="false", we need to add cases to all policy assertions xslt we made so far.
    
    Change Logs:
    1. fixed default value output
    3. fixed typing error about ture/false (from T/F)
    2. add namespaces definition in every MD output part
    3. normalize spaces (removing leading or trailing spaces) of ontologicalMeaning
    4. fixed output if there's only 1 parameter for eventDefinition
    5. sequence number added
    6. add copying all content in configurationData in config file to output
    7. add a temporary root element in output xml for well-formed.
    8. namespaces finished
    9. problem that namespace URI would appear inside element, like (<masc-ex:boolean="...." xmlns:masc-ex=".........">), is fixed
    10. Boolean Expression is finished and tested for different cases (please check if the example in each cases I inclued in comment is in right structure)
    11. fix some typing errors of name convention
    12. add optional schedule parts
    
    
    Additional Info:
    1. there's no output for PassedInSOAPHeaders for example. since there's no corresponding GPA-Additioanl Info in config file
    2. I found that for character like '<', '>', '"', etc, there're 2 sets of entity references (e.g. for '<', could be '&lt;' or '&#60;')
    check here for further info (http://en.wikipedia.org/wiki/Character_entity_reference)
    I think we shoud use xml standard which are: &lt;, &gt;, &amp;, &quot;, or &apos;, 
    but just for the example, we use &#x201d; for getting right result, in the future we can replace all &#201d; with &quod; and make a note for that
    3. WhenFor is not defined yet, since it's not a big issue, we will consider that later.
        
    ****************************************************************************
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:MASC_Profile="http:///schemas/MASC_Profile/_OnYS4BTgEd6yO-ZURVEtWw/6"
	xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
	xmlns:uml="http://www.eclipse.org/uml2/2.1.0/UML"
	xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
	xmlns:masc-gp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp"
	xmlns:masc-se="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se"
	xmlns:masc-ex="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex"
	xmlns:masc-om="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om"
	xmlns:masc-sc="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc"
	xmlns:masc-ap="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap"
	xmlns:masc-mp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp"
	xmlns:masc-up="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up"
	xmlns:masc-config="http://www.nicta.com.au/projects/armature/masc-config"
	xmlns:wsa="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/wsa"
	xmlns:QoSOntology="http://www.myontologyrepository.com/qosontology.owl"
	xmlns:tns="${currentFileURL}" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="xs" version="2.0">

	<xsl:output method="xml" indent="yes"/>

	<xsl:variable name="input" select="document('../inputs/StockTradingGPA.xml')"/>
	<xsl:variable name="config" select="document('../inputs/config.xml')"/>

	<!-- define a newline variable for pretty output -->
	<xsl:variable name="newline">
		<xsl:text>&#10;</xsl:text>
	</xsl:variable>
	<!-- define the target namespace -->
	<xsl:variable name="tns">
		<xsl:text>tns:</xsl:text>
	</xsl:variable>

	<xsl:template match="/" name="GPA_Trans">
		<xsl:element name="root">
			<xsl:comment>
				<xsl:text>This root element is for well-formed of output. Could be removed if we generate a corresponding output file with every Policy Assertion/Monitored Definition</xsl:text>
			</xsl:comment>
			<xsl:value-of select="$newline"/>
			<xsl:value-of select="$newline"/>
			<xsl:apply-templates select="$input/xmi:XMI"/>
		</xsl:element>
		<xsl:value-of select="$newline"/>
	</xsl:template>


	<xsl:template match="xmi:XMI">
		<xsl:for-each select="$input/xmi:XMI/MASC_Profile:Goal_PA">
			<xsl:variable name="GPA_booleanExp" select="replace(./@BooleanExpression, '\s+', '')"/>
			<xsl:variable name="responsibleParty" select="./@ResponsibleParty"/>
			<xsl:variable name="gp-When-TriggerEvents" select="./@GP-When-TriggerEvents"/>
			<xsl:variable name="gp-MASCID" select="./@GP-MASCID"/>
			<xsl:variable name="gp-managementParty" select="./@GP-ManagementParty"/>
			<xsl:variable name="baseElement" select="./@base_Element"/>
			<xsl:variable name="SN">
				<!-- define sequence number by MD's position -->
				<xsl:number value="position()" format="1"/>
			</xsl:variable>


			<xsl:for-each select="$input//packagedElement/node">
				<xsl:variable name="name" select="@name"/>
				<xsl:variable name="xmiID" select="@xmi:id"/>

				<xsl:choose>
					<xsl:when test="$xmiID=$baseElement">

						<!--
                            ***********************************************************

                                    varibales for goal policy assertion constructs

                            ***********************************************************
                        -->

						<!-- variables for states -->
						<xsl:variable name="GPA_SD">
							<!-- state definition -->
							<xsl:value-of
								select="substring-before(substring-after($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/@When-AllowedStates, '{'), '}')"
							/>
						</xsl:variable>

						<!-- variables for events -->
						<xsl:variable name="GPA_temp1">
							<xsl:value-of select="$gp-When-TriggerEvents"/>
						</xsl:variable>
						<xsl:variable name="GPA_ET">
							<!-- event type -->
							<xsl:value-of
								select="substring-after(substring-before($GPA_temp1, '('), '{')"/>
						</xsl:variable>

						<xsl:variable name="GPA_temp2">
							<xsl:value-of
								select="substring-after(substring-before($GPA_temp1, ')'), '(')"/>
						</xsl:variable>
						<xsl:variable name="GPA_Event_ParamList" select="tokenize($GPA_temp2,',')"/>
						<xsl:variable name="GPA_Event_P1">
							<xsl:value-of
								select="normalize-space($GPA_Event_ParamList[position()=1])"/>
						</xsl:variable>
						<xsl:variable name="GPA_Event_P2">
							<xsl:value-of
								select="normalize-space($GPA_Event_ParamList[position()=2])"/>
						</xsl:variable>
						<xsl:variable name="GPA_Event_P3">
							<xsl:value-of
								select="normalize-space($GPA_Event_ParamList[position()=3])"/>
						</xsl:variable>
						<xsl:variable name="GPA_Event_P4">
							<xsl:value-of
								select="normalize-space($GPA_Event_ParamList[position()=4])"/>
						</xsl:variable>
						<xsl:variable name="GPA_Event_P5">
							<xsl:value-of
								select="normalize-space($GPA_Event_ParamList[position()=5])"/>
						</xsl:variable>
						<xsl:variable name="GPA_Event_P6">
							<xsl:value-of
								select="normalize-space($GPA_Event_ParamList[position()=6])"/>
						</xsl:variable>
						<xsl:variable name="GPA_Event_ParamCount">
							<xsl:value-of select="count($GPA_Event_ParamList)"/>
						</xsl:variable>

						<xsl:variable name="PN">
							<!-- process name -->
							<xsl:value-of
								select="$config/masc-config:XMIFile/masc-config:UMLDiagram/@UMLDiagramName"
							/>
						</xsl:variable>
						<xsl:variable name="AN">
							<!-- activity name -->
							<xsl:value-of
								select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/@UMLElementName"
							/>
						</xsl:variable>

						<!-- variables for when constructs -->
						<xsl:variable name="GPA_WF">
							<!-- When For -->
							<!-- not finished -->
							<xsl:text>WF</xsl:text>
						</xsl:variable>
						<xsl:variable name="GPA_WFMASCID">
							<!-- value of WhenFor MASCID -->
							<!-- not finished -->
							<!-- user GP-MASCID-GPA_ET for now -->
							<xsl:value-of select="$gp-MASCID"/>
							<xsl:text>-</xsl:text>
							<xsl:value-of select="$GPA_ET"/>
						</xsl:variable>
						<xsl:variable name="GPA_ED">
							<!-- mascid of previously defined Event Definition Construct -->
							<xsl:value-of select="$GPA_ET"/>
							<xsl:text>-</xsl:text>
							<xsl:value-of select="$GPA_Event_P1"/>
							<xsl:if test="normalize-space($GPA_Event_P2)!=''">
								<xsl:text>-</xsl:text>
								<xsl:value-of select="$GPA_Event_P2"/>
							</xsl:if>
						</xsl:variable>

						<!-- variables for boolean expressions for monitoring definition are in their templates based on different cases -->

						<!-- variables for definitions of goal policy assertions -->

						<!-- variables for NameSpaces of GPA -->
						<xsl:variable name="GPA_TNS">
							<xsl:value-of select="$PN"/>
							<xsl:text>-GPA-WSP4MASC</xsl:text>
						</xsl:variable>

						<!--
                            ***********************************************************
                            
                            		Define output for GPA
                            
                            ***********************************************************
                        -->
						<xsl:comment>
							<xsl:text>OUTPUT FOR Goal Policy Assertion for UML Element: </xsl:text>
							<xsl:value-of select="$name"/>
							<xsl:value-of select="$newline"/>
							<xsl:text> The MASCID is: </xsl:text>
							<xsl:value-of select="$gp-MASCID"/>
						</xsl:comment>
						<xsl:value-of select="$newline"/>

						<xsl:element name="wsp:Policy">
							<!-- constant namespaces -->
							<xsl:namespace name="xs" select="'http://www.w3.org/2001/XMLSchema'"/>
							<xsl:namespace name="wsp"
								select="'http://schemas.xmlsoap.org/ws/2004/09/policy'"/>
							<xsl:namespace name="masc-gp"
								select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp'"/>
							<xsl:namespace name="masc-se"
								select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se'"/>
							<xsl:namespace name="masc-ex"
								select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex'"/>
							<xsl:namespace name="masc-om"
								select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om'"/>
							<xsl:namespace name="masc-sc"
								select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc'"/>
							<xsl:namespace name="masc-ap"
								select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap'"/>
							<xsl:namespace name="masc-mp"
								select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp'"/>
							<xsl:namespace name="masc-up"
							select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up'"/>
							<xsl:namespace name="masc-config" select="'http://www.nicta.com.au/projects/armature/masc-config'"/>
							<xsl:namespace name="wsa" select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/wsa'"/>
							<!-- schema locations -->
							<xsl:namespace name="xsi"
								select="'http://www.w3.org/2001/XMLSchema-instance'"/>
							<xsl:namespace name="schemaLocation"
								select="'http://schemas.xmlsoap.org/ws/2004/09/policy 
                    			C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy.xsd 
                    			http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp 
                    			C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-gp.xsd
                    			http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se 
                    			C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-se.xsd
                    			http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex 
                    			C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ex.xsd 
                    			http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om 
                    			C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-sc.xsd 
                    			http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc 
                    			C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-om.xsd 
                    			http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap 
                    			C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ap.xsd 
                    			http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up 
                    			C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-up.xsd 
                    			http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp 
                    			C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-mp.xsd'"/>
							<!-- namespaces for TargetNameSpace -->
							<xsl:namespace name="targetNamespace" select="$GPA_TNS"/>
							<xsl:namespace name="tns" select="$GPA_TNS"/>
							<!-- namespaces for QoSOntology, MeasurementOntology and WSDLs -->
							<xsl:namespace name="QoSOntology"
								select="$config/masc-config:XMIFile/namespace::*[normalize-space(lower-case(local-name()))='qosontology']"/>
							<xsl:namespace name="MeasurementOntology"
								select="$config/masc-config:XMIFile/namespace::*[normalize-space(lower-case(local-name()))='measurementontology']"/>
							<xsl:for-each
								select="$config/masc-config:XMIFile/namespace::*[starts-with(normalize-space(lower-case(local-name())), 'wsdl')]">
								<xsl:namespace name="{name()}">
									<xsl:value-of select="."/>
								</xsl:namespace>
							</xsl:for-each>
							<xsl:value-of select="$newline"/>


							<xsl:comment>
								<xsl:text>Definition of States</xsl:text>
							</xsl:comment>
							<xsl:value-of select="$newline"/>
							<xsl:element name="masc-se:StateDefinition">
								<xsl:attribute name="MASCID">
									<!-- MASCID="GPA_SD" -->
									<xsl:value-of select="$GPA_SD"/>
								</xsl:attribute>
							</xsl:element>
							<xsl:value-of select="$newline"/>
							<xsl:value-of select="$newline"/>

							<!-- event constructs -->
							<xsl:comment>
								<xsl:text>Definition of Events</xsl:text>
							</xsl:comment>
							<xsl:value-of select="$newline"/>
							<xsl:element name="masc-se:EventDefinition">
								<xsl:attribute name="MASCID">
									<!-- MASCID="GPA_ET-GPA_Event_P1-GPA_Event_P2", up to 2 parameters -->
									<xsl:value-of select="$GPA_ED"/>
									<xsl:text>-</xsl:text>
									<xsl:value-of select="$SN"/>
								</xsl:attribute>
								<!-- cases of events -->
								<xsl:choose>
									<!-- case1: ActivityEnded or ActivityStarted -->
									<xsl:when test="contains(lower-case($GPA_ET), 'activityended')">
										<xsl:element name="masc-se:{$GPA_ET}">
											<xsl:attribute name="ActivityName">
												<xsl:value-of select="$AN"/>
											</xsl:attribute>
											<xsl:attribute name="Successful">
												<xsl:value-of select="$GPA_Event_P2"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<xsl:when
										test="contains(lower-case($GPA_ET), 'activitystarted')">
										<xsl:element name="masc-se:{$GPA_ET}">
											<xsl:attribute name="ActivityName">
												<xsl:value-of select="$AN"/>
											</xsl:attribute>
											<xsl:attribute name="Successful">
												<xsl:value-of select="$GPA_Event_P2"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<!-- case2: MonitoredDataItemUpdated -->
									<xsl:when
										test="contains(lower-case($GPA_ET), 'monitoreddataitemupdated')">
										<xsl:element name="masc-se:MonitoredDataItemUpdated">
											<xsl:attribute name="ItemID">
												<xsl:value-of select="$tns"/>
												<xsl:value-of select="$GPA_Event_P1"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<!-- case3: GoalStatisfaction -->
									<xsl:when
										test="contains(lower-case($GPA_ET), 'goalsatisfaction')">
										<xsl:element name="masc-se:GoalStatisfaction">
											<xsl:attribute name="GoalPolicyAssertionID">
												<xsl:value-of select="$tns"/>
												<xsl:value-of select="$GPA_Event_P1"/>
											</xsl:attribute>
											<xsl:attribute name="Satisfied">
												<xsl:value-of select="$GPA_Event_P2"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<!-- case4: OperationInvocation -->
									<xsl:when
										test="contains(lower-case($GPA_ET), 'operationinvocation')">
										<xsl:element name="masc-se:OperationInvocation">
											<xsl:element name="masc-sc:ScopeOperation">
												<xsl:element name="masc-sc:ServiceName">
												<xsl:attribute name="Name">
												<xsl:value-of select="$GPA_Event_P1"/>
												<xsl:text>:</xsl:text>
												<xsl:value-of select="$GPA_Event_P2"/>
												</xsl:attribute>
												<xsl:attribute name="Completed">
												<xsl:value-of select="$GPA_Event_P5"/>
												</xsl:attribute>
												<xsl:attribute name="FaultsOccured">
												<xsl:value-of select="$GPA_Event_P6"/>
												</xsl:attribute>
												</xsl:element>
												<xsl:element name="masc-sc:EndpointName">
												<xsl:attribute name="Name">
												<xsl:value-of select="$GPA_Event_P1"/>
												<xsl:text>:</xsl:text>
												<xsl:value-of select="$GPA_Event_P3"/>
												</xsl:attribute>
												</xsl:element>
												<xsl:element name="masc-sc:OperationName">
												<xsl:attribute name="Name">
												<xsl:value-of select="$GPA_Event_P1"/>
												<xsl:text>:</xsl:text>
												<xsl:value-of select="$GPA_Event_P4"/>
												</xsl:attribute>
												</xsl:element>
											</xsl:element>
										</xsl:element>
									</xsl:when>
									<!-- case5: MessageReceipt, MessageToBeSent and MessageSending -->
									<xsl:when test="starts-with(lower-case($GPA_ET), 'message')">
										<xsl:element name="masc-se:{$GPA_ET}">
											<xsl:element name="masc-sc:ScopeMessage">
												<xsl:element name="masc-sc:ServiceName">
												<xsl:attribute name="Name">
												<xsl:value-of select="$GPA_Event_P1"/>
												<xsl:text>:</xsl:text>
												<xsl:value-of select="$GPA_Event_P2"/>
												</xsl:attribute>
												<xsl:attribute name="Completed">
												<xsl:value-of select="$GPA_Event_P5"/>
												</xsl:attribute>
												</xsl:element>
												<xsl:element name="masc-sc:EndpointName">
												<xsl:attribute name="Name">
												<xsl:value-of select="$GPA_Event_P1"/>
												<xsl:text>:</xsl:text>
												<xsl:value-of select="$GPA_Event_P3"/>
												</xsl:attribute>
												</xsl:element>
												<xsl:element name="masc-sc:OperationName">
												<xsl:attribute name="Name">
												<xsl:value-of select="$GPA_Event_P1"/>
												<xsl:text>:</xsl:text>
												<xsl:value-of select="$GPA_Event_P4"/>
												</xsl:attribute>
												</xsl:element>
												<xsl:element name="masc-sc:MessageName">
												<xsl:attribute name="Name">
												<xsl:value-of select="$GPA_Event_P5"/>
												</xsl:attribute>
												</xsl:element>
											</xsl:element>
										</xsl:element>
									</xsl:when>
									<!-- case6: ActionCompletion -->
									<xsl:when
										test="contains(lower-case($GPA_ET), 'actioncompletion')">
										<xsl:element name="masc-se:ActionCompletion">
											<xsl:attribute name="ActionPolicyAssertionID">
												<xsl:value-of select="$tns"/>
												<xsl:value-of select="$GPA_Event_P1"/>
											</xsl:attribute>
											<xsl:attribute name="Completed">
												<xsl:value-of select="$GPA_Event_P2"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<!-- case7: ScheduledEventOccurence -->
									<xsl:when
										test="contains(lower-case($GPA_ET), 'scheduledeventoccurence')">
										<xsl:element name="masc-se:ScheduledEventOccurence">
											<xsl:attribute name="scheduleID">
												<xsl:value-of select="$tns"/>
												<xsl:value-of select="$GPA_Event_P1"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<!-- case8: ProcessInstanceStart and ProcessInstanceFinished -->
									<xsl:when
										test="starts-with(lower-case($GPA_ET), 'processinstance')">
										<xsl:element name="masc-se:PIT">
											<xsl:attribute name="ProcessType">
												<xsl:value-of select="$tns"/>
												<xsl:value-of select="$GPA_Event_P1"/>
											</xsl:attribute>
											<xsl:attribute name="Completed">
												<xsl:value-of select="$GPA_Event_P2"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<!-- case9: PartyToBeInitialized and PartyToBeFinalized -->
									<xsl:when test="starts-with($GPA_ET, 'partytobe')">
										<xsl:element name="masc-se:PIF">
											<xsl:attribute name="PartyName">
												<xsl:value-of select="$tns"/>
												<xsl:value-of select="$GPA_Event_P1"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<!-- case10: StateEntrance, StateTransitioning and StateExit -->
									<xsl:when
										test="(contains(lower-case($GPA_ET), 'stateentrance')) or (contains(lower-case($GPA_ET),'statetransitioning')) or (contains(lower-case($GPA_ET), 'stateexit'))">
										<xsl:element name="masc-se:STTE">
											<xsl:attribute name="To">
												<xsl:value-of select="$tns"/>
												<xsl:value-of select="$GPA_Event_P1"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<!-- case11: PolicyAssertionAddition and PolicyAssertionDeletion -->
									<xsl:when
										test="starts-with(lower-case($GPA_ET), 'policyassertion')">
										<xsl:element name="masc-se:PT">
											<xsl:attribute name="PolicyAssertionID">
												<xsl:value-of select="$tns"/>
												<xsl:value-of select="$GPA_Event_P1"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
									<!-- case12: MonitoredContextPropertyChanged -->
									<xsl:when
										test="contains(lower-case($GPA_ET), 'monitoredcontextpropertychanged')">
										<xsl:element name="masc-se:MonitoredContextPropertyChanged">
											<xsl:attribute name="ContextPropertyID">
												<xsl:value-of select="$tns"/>
												<xsl:value-of select="$GPA_Event_P1"/>
											</xsl:attribute>
										</xsl:element>
									</xsl:when>
								</xsl:choose>
								<!-- cases end -->
							</xsl:element>
							<xsl:value-of select="$newline"/>
							<xsl:value-of select="$newline"/>

							<!-- When Constructs  -->
							<xsl:comment>
								<xsl:text>Definition of When Constructs</xsl:text>
							</xsl:comment>
							<xsl:value-of select="$newline"/>
							<xsl:element name="masc-se:When">
								<xsl:attribute name="MASCID">
									<!-- MASCID="GPA_WF-GPA_WFMASCID" -->
									<xsl:value-of select="$GPA_WF"/>
									<xsl:text>-</xsl:text>
									<xsl:value-of select="$GPA_WFMASCID"/>
								</xsl:attribute>
								<xsl:element name="masc-se:AllowedStates">
									<xsl:element name="masc-se:StateRef">
										<xsl:attribute name="To">
											<!-- StateRef To="tns:GPA_SD" -->
											<xsl:value-of select="$tns"/>
											<xsl:value-of select="$GPA_SD"/>
										</xsl:attribute>
									</xsl:element>
								</xsl:element>
								<xsl:element name="masc-se:PossibleTriggerEvents">
									<xsl:element name="masc-se:EventRef">
										<xsl:attribute name="To">
											<!-- EventRef To="tns:GPA_ED" -->
											<xsl:value-of select="$tns"/>
											<xsl:value-of select="$GPA_ED"/>
										</xsl:attribute>
									</xsl:element>
								</xsl:element>
								<xsl:element name="masc-se:BooleanExpression">
									<xsl:element name="masc-se:BooleanExpressionRef">
										<xsl:attribute name="To">
											<xsl:value-of select="$tns"/>
											<!-- BooleanExp MASCID varies for cases -->
											<!-- TODO: should be more easier after tns done -->
											<xsl:choose>
												<!-- case1. Boolean Constants  -->
												<xsl:when
												test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition[./True or ./False]">
												<xsl:text>BC-</xsl:text>
												<xsl:value-of select="$gp-MASCID"/>
												</xsl:when>
												<!-- case2 just 1 comparisions -->
												<xsl:when
												test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition[./Equal or ./NotEqual or ./Less or ./LessOrEqual or ./Greater or ./GreaterOrEqual]">
												<xsl:text>BE-</xsl:text>
												<xsl:value-of select="$gp-MASCID"/>
												<xsl:text>-</xsl:text>
												<xsl:value-of
												select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition//MonitoredData/@Name"/>
												<xsl:text>-</xsl:text>
												<xsl:value-of
												select="replace(generate-id($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition//MonitoredData), '([a-z]|[A-Z])', '')"
												/>
												</xsl:when>
												<!-- case3 Comparator with 2 comparisions -->
												<xsl:when
												test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition[./AND or ./OR]">
												<xsl:text>BE-</xsl:text>
												<xsl:value-of
												select="node-name($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition/*)"/>
												<xsl:text>-</xsl:text>
												<!-- generate id for ./OT (e.g. WhenFilteringCondition/AND) as sequence number -->
												<xsl:value-of
												select="replace(generate-id($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition/*), '([a-z]|[A-Z])', '')"
												/>
												</xsl:when>
												<!-- case4 This case would not appear in AdditionalInfo -->
												<xsl:when
												test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition[./BoolEventAttributeIsTrue or ./BoolEventAttributeIsFalse]">
												<xsl:text>BEV-</xsl:text>
												<xsl:value-of
												select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition/*/@EventRef"/>
												<xsl:text>-</xsl:text>
												<xsl:value-of
												select="replace(generate-id($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition/*), '([a-z]|[A-Z])', '')"
												/>
												</xsl:when>
											</xsl:choose>
										</xsl:attribute>
									</xsl:element>
								</xsl:element>
							</xsl:element>
							<xsl:value-of select="$newline"/>
							<xsl:value-of select="$newline"/>

							<!-- boolean expressions constructs from WhenFilteringCondition -->
							<xsl:comment>
								<xsl:text>Definition of BooleanExpressions from WhenFilteringCondition</xsl:text>
							</xsl:comment>
							<xsl:value-of select="$newline"/>
							<!-- could add a for loop for multiple When-FilteringCondition -->
							<xsl:choose>
								<!-- if can not find MD Additional Info for selected stereotype -->
								<xsl:when
									test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')][not(masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID])]">
									<xsl:text>Can not find corresponding MD Additional Info in config file</xsl:text>
								</xsl:when>
								<xsl:otherwise>
									<xsl:apply-templates
										select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition"
									/>
								</xsl:otherwise>
							</xsl:choose>
							<xsl:value-of select="$newline"/>
							<xsl:value-of select="$newline"/>

							<!-- TODO: boolean expressions constructs from GPA BooleanExpression Tagged Value -->
							<xsl:comment>
								<xsl:text>Definition of BooleanExpressions from GPA BooleanExpression</xsl:text>
							</xsl:comment>
							<xsl:value-of select="$newline"/>
							<!-- set a variable OT which is the first element tag inside BooleanExp -->
							<xsl:variable name="OT"
								select="substring-before(substring-after($GPA_booleanExp, '&lt;'), '>')"/>
							<!-- set a variable secondElement which is the 2nd element tag inside BooleanExp, if any -->
							<xsl:variable name="secondElement"
								select="substring-before(substring-after($GPA_booleanExp, '>&lt;'), '>')"/>
							<!-- set variables for 1st comparision, if any -->
							<xsl:variable name="firstMDIN"
								select="substring-before(substring-after($GPA_booleanExp, 'MonitoredDataName=&#x201d;'), '&#x201d;')"/>
							<xsl:variable name="firstAwUExpValue"
								select="substring-before(substring-after($GPA_booleanExp, 'AwUConstValue=&#x201d;'), '&#x201d;')"/>
							<xsl:variable name="firstAwUExpUnit"
								select="substring-before(substring-after($GPA_booleanExp, 'Unit=&#x201d;'), '&#x201d;')"/>
							<!-- set variables for 2nd comparision, if any -->
							<xsl:variable name="tempMDIN">
								<xsl:call-template name="substring-after-last">
									<xsl:with-param name="string" select="$GPA_booleanExp"/>
									<xsl:with-param name="delimiter"
										select="'MonitoredDataName=&#x201d;'"/>
								</xsl:call-template>
							</xsl:variable>
							<xsl:variable name="tempAV">
								<xsl:call-template name="substring-after-last">
									<xsl:with-param name="string" select="$GPA_booleanExp"/>
									<xsl:with-param name="delimiter"
										select="'AwUConstValue=&#x201d;'"/>
								</xsl:call-template>
							</xsl:variable>
							<xsl:variable name="tempUnit">
								<xsl:call-template name="substring-after-last">
									<xsl:with-param name="string" select="$GPA_booleanExp"/>
									<xsl:with-param name="delimiter" select="'Unit=&#x201d;'"/>
								</xsl:call-template>
							</xsl:variable>
							<xsl:variable name="secondMDIN"
								select="substring-before($tempMDIN, '&#x201d;')"/>
							<xsl:variable name="secondAwUExpValue"
								select="substring-before($tempAV, '&#x201d;')"/>
							<xsl:variable name="secondAwUExpUnit"
								select="substring-before($tempUnit, '&#x201d;')"/>
							<!-- case1. Boolean Constants
								e.g.
								<When-Filtering-Condition>
								<True/>
								</When-Filtering-Condition> 
							-->
							<xsl:if
								test="starts-with($GPA_booleanExp, '&lt;True') or starts-with($GPA_booleanExp, '&lt;False')">
								<xsl:element name="masc-ex:BooleanExpression">
									<xsl:attribute name="MASCID">
										<xsl:text>BC-</xsl:text>
										<xsl:value-of select="$gp-MASCID"/>
									</xsl:attribute>
									<xsl:element name="masc-ex:BooleanConstant">
										<xsl:attribute name="Type" select="lower-case(string($OT))"
										/>
									</xsl:element>
								</xsl:element>
							</xsl:if>

							<!-- case2 just 1 comparisions -->
							<!-- if the first element tag name is Equal, NotEqual, Less, etc -->
							<xsl:if
								test="$OT='Equal' or $OT='NotEqual' or $OT='Less' or $OT='LessOrEqual' or $OT='Greater' or $OT='GreaterOrEqual'">
								<xsl:choose>
									<!-- case2.1 with Operater in AwUExpression
										e.g.
										<Equal>
										<Add>
										<MonitoredData Name=""/>
										<AwUConst Value="" Unit=""/>
										</Add>
										</Equal>   
									-->
									<!-- test if second element tag in BooleanExp is Add or Multiply -->
									<xsl:when
										test="$secondElement='Add' or $secondElement='Multiply'">
										<xsl:call-template name="Comparision">
											<xsl:with-param name="GPA_MASCID" select="$gp-MASCID"/>
											<xsl:with-param name="C" select="$OT"/>
											<xsl:with-param name="MDIN" select="$firstMDIN"/>
											<xsl:with-param name="operater" select="$secondElement"/>
											<xsl:with-param name="AwUExpValue"
												select="$firstAwUExpValue"/>
											<xsl:with-param name="SN_MonitoredData" select="$SN"/>
											<xsl:with-param name="SN_AwUConst" select="$SN"/>
											<xsl:with-param name="OURI"
												select="substring-before($firstAwUExpUnit, ':')"/>
											<xsl:with-param name="T"
												select="substring-after($firstAwUExpUnit, ':')"/>
										</xsl:call-template>
									</xsl:when>
									<!-- case2.2 without Operater inside AwUExpression
										e.g.
										<Equal>
										<MonitoredData Name=""/>
										<AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>)
										</Equal>   
									-->
									<xsl:otherwise>
										<xsl:call-template name="Comparision">
											<xsl:with-param name="GPA_MASCID" select="$gp-MASCID"/>
											<xsl:with-param name="MDIN" select="$firstMDIN"/>
											<xsl:with-param name="C" select="$OT"/>
											<xsl:with-param name="AwUExpValue"
												select="$firstAwUExpValue"/>
											<xsl:with-param name="AwUExpUnit"
												select="$firstAwUExpUnit"/>
											<xsl:with-param name="OURI"
												select="substring-before($firstAwUExpUnit, ':')"/>
											<xsl:with-param name="T"
												select="substring-after($firstAwUExpUnit, ':')"/>
											<xsl:with-param name="SN_MonitoredData" select="$SN"/>
											<xsl:with-param name="SN_AwUConst" select="$SN"/>
										</xsl:call-template>
									</xsl:otherwise>
								</xsl:choose>
							</xsl:if>

							<!-- case3 Comparator with 2 comparisions 
								e.g.
								&lt;AND>&lt;Equal>&lt;Add>&lt;MonitoredDataName=&quot;...&quot;/>&lt;AwUConstValue=&quot...&quot;/>&lt;/Add>&lt;/Equal>
								&lt;Equal>&lt;Add>&lt;MonitoredDataName=&quot;...&quot;/>&lt;AwUConstValue=&quot...&quot;/>&lt;/Add>&lt;/Equal>&lt;/AND>
							-->
							<xsl:if test="$OT='AND' or $OT='OR'">
								<xsl:element name="masc-ex:BooleanExpression">
									<xsl:attribute name="MASCID">
										<xsl:text>BE-</xsl:text>
										<xsl:value-of select="$OT"/>
										<xsl:text>-</xsl:text>
										<xsl:value-of select="$SN"/>
									</xsl:attribute>
									<xsl:element name="masc-ex:BinaryBooleanOperator">
										<xsl:attribute name="Type" select="$OT"/>
									</xsl:element>
									<!-- first comparision AwU has operater -->
									<xsl:variable name="firstOperater">
										<xsl:choose>
											<!-- if before first comparision, we still find another open tag of operater (e.g. Add, Multiply)
												then we set this first operater to a variable
											-->
											<xsl:when
												test="contains(substring-before($GPA_booleanExp, 'AwUConstValue='), '&lt;Add>')">
												<xsl:text>Add</xsl:text>
											</xsl:when>
											<xsl:when
												test="contains(substring-before($GPA_booleanExp, 'AwUConstValue='), '&lt;Multiply>')">
												<xsl:text>Multiply</xsl:text>
											</xsl:when>
										</xsl:choose>
									</xsl:variable>
									<xsl:choose>
										<!-- case3.1 with Operater inside AwUExpression
												e.g.
												<When-Filtering-Condition>
												<AND>
												<Equal>
												<Add>
												<MonitoredData Name=""/>
												<AwUConst Value="" Unit=""/>
												</Add>
												</Equal>
												<NotEqual>
												<Multiply>
												<MonitoredData Name=""/>
												<AwUConst Value=""/>
												<Multiply>
												</NotEqual>
												</AND>
												</When-Filtering-Condition>
											-->
										<xsl:when
											test="$firstOperater='Add' or $firstOperater='Multiply'">
											<xsl:call-template name="Comparision">
												<xsl:with-param name="GPA_MASCID"
												select="$gp-MASCID"/>
												<xsl:with-param name="C" select="$OT"/>
												<xsl:with-param name="MDIN" select="$firstMDIN"/>
												<xsl:with-param name="operater"
												select="$firstOperater"/>
												<xsl:with-param name="AwUExpValue"
												select="$firstAwUExpValue"/>
												<xsl:with-param name="SN_MonitoredData">
												<xsl:value-of select="$SN"/>
												<xsl:text>1</xsl:text>
												</xsl:with-param>
												<xsl:with-param name="SN_AwUConst">
												<xsl:value-of select="$SN"/>
												<xsl:text>1</xsl:text>
												</xsl:with-param>
												<xsl:with-param name="OURI"
												select="substring-before($firstAwUExpUnit, ':')"/>
												<xsl:with-param name="T"
												select="substring-after($firstAwUExpUnit, ':')"/>
											</xsl:call-template>
										</xsl:when>
										<!-- case3.2 without Operater inside AwUExpression
												e.g.
												<When-Filtering-Condition>
												<AND>
												<Equal>
												<MonitoredData Name=""/>
												<AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>
												</Equal>
												<NotEqual>
												<MonitoredData Name=""/>
												<AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>
												</NotEqual>
												</AND>
												</When-Filtering-Condition>
											-->
										<!-- first comparision AwU has no operater -->
										<xsl:otherwise>
											<xsl:call-template name="Comparision">
												<xsl:with-param name="GPA_MASCID"
												select="$gp-MASCID"/>
												<xsl:with-param name="MDIN" select="$firstMDIN"/>
												<xsl:with-param name="C" select="$OT"/>
												<xsl:with-param name="AwUExpValue"
												select="$firstAwUExpValue"/>
												<xsl:with-param name="AwUExpUnit"
												select="$firstAwUExpUnit"/>
												<xsl:with-param name="OURI"
												select="substring-before($firstAwUExpUnit, ':')"/>
												<xsl:with-param name="T"
												select="substring-after($firstAwUExpUnit, ':')"/>
												<xsl:with-param name="SN_MonitoredData">
												<xsl:value-of select="$SN"/>
												<xsl:text>1</xsl:text>
												</xsl:with-param>
												<xsl:with-param name="SN_AwUConst">
												<xsl:value-of select="$SN"/>
												<xsl:text>1</xsl:text>
												</xsl:with-param>
											</xsl:call-template>
										</xsl:otherwise>
									</xsl:choose>
									<xsl:variable name="secondOperater">
										<xsl:choose>
											<!-- if after first comparision, we still find another open tag of operater (e.g. Add, Multiply)
												then we set this second operater to a variable
											-->
											<xsl:when
												test="contains(substring-after($GPA_booleanExp, 'AwUConstValue='), '&lt;Add>')">
												<xsl:text>Add</xsl:text>
											</xsl:when>
											<xsl:when
												test="contains(substring-after($GPA_booleanExp, 'AwUConstValue='), '&lt;Multiply>')">
												<xsl:text>Multiply</xsl:text>
											</xsl:when>
										</xsl:choose>
									</xsl:variable>
									<xsl:choose>
										<!-- case3.1 with Operater inside AwUExpression
											e.g.
											<When-Filtering-Condition>
											<AND>
											<Equal>
											<Add>
											<MonitoredData Name=""/>
											<AwUConst Value="" Unit=""/>
											</Add>
											</Equal>
											<NotEqual>
											<Multiply>
											<MonitoredData Name=""/>
											<AwUConst Value=""/>
											<Multiply>
											</NotEqual>
											</AND>
											</When-Filtering-Condition>
										-->
										<!-- second comparision AwU has operater -->
										<xsl:when
											test="$secondOperater='Add' or $secondOperater='Multiply'">
											<xsl:call-template name="Comparision">
												<xsl:with-param name="GPA_MASCID"
												select="$gp-MASCID"/>
												<xsl:with-param name="C" select="$OT"/>
												<xsl:with-param name="MDIN" select="$secondMDIN"/>
												<xsl:with-param name="operater"
												select="$secondOperater"/>
												<xsl:with-param name="AwUExpValue"
												select="$secondAwUExpValue"/>
												<xsl:with-param name="SN_MonitoredData">
												<xsl:value-of select="$SN"/>
												<xsl:text>2</xsl:text>
												</xsl:with-param>
												<xsl:with-param name="SN_AwUConst">
												<xsl:value-of select="$SN"/>
												<xsl:text>2</xsl:text>
												</xsl:with-param>
												<xsl:with-param name="OURI"
												select="substring-before($secondAwUExpUnit, ':')"/>
												<xsl:with-param name="T"
												select="substring-after($secondAwUExpUnit, ':')"/>
											</xsl:call-template>
										</xsl:when>
										<!-- case3.2 without Operater inside AwUExpression
											e.g.
											<When-Filtering-Condition>
											<AND>
											<Equal>
											<MonitoredData Name=""/>
											<AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>
											</Equal>
											<NotEqual>
											<MonitoredData Name=""/>
											<AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>
											</NotEqual>
											</AND>
											</When-Filtering-Condition>
										-->
										<!-- second comparision AwU has no operater -->
										<xsl:otherwise>
											<xsl:call-template name="Comparision">
												<xsl:with-param name="GPA_MASCID"
												select="$gp-MASCID"/>
												<xsl:with-param name="MDIN" select="$secondMDIN"/>
												<xsl:with-param name="C" select="$OT"/>
												<xsl:with-param name="AwUExpValue"
												select="$secondAwUExpValue"/>
												<xsl:with-param name="AwUExpUnit"
												select="$secondAwUExpUnit"/>
												<xsl:with-param name="OURI"
												select="substring-before($secondAwUExpUnit, ':')"/>
												<xsl:with-param name="T"
												select="substring-after($secondAwUExpUnit, ':')"/>
												<xsl:with-param name="SN_MonitoredData">
												<xsl:value-of select="$SN"/>
												<xsl:text>2</xsl:text>
												</xsl:with-param>
												<xsl:with-param name="SN_AwUConst">
												<xsl:value-of select="$SN"/>
												<xsl:text>2</xsl:text>
												</xsl:with-param>
											</xsl:call-template>
										</xsl:otherwise>
									</xsl:choose>
								</xsl:element>
							</xsl:if>

							<!-- case4 This case would not appear in AdditionalInfo -->
							<xsl:if
								test="$OT='BoolEventAttributeIsTrue' or $OT='BoolEventAttributeIsFalse'">
								<xsl:variable name="V1"
									select="substring-before(substring-after($GPA_booleanExp, 'EventRef=&quot;'), '&quot;')"/>
								<xsl:variable name="V2"
									select="substring-before(substring-after($GPA_booleanExp, 'Attribute=&quot;'), '&quot;')"/>
								<xsl:choose>
									<xsl:when test="$OT='BoolEventAttributeIsTrue'">
										<xsl:call-template name="EventSatisfied">
											<xsl:with-param name="Boolean" select="'True'"/>
											<xsl:with-param name="V1" select="$V1"/>
											<xsl:with-param name="V2" select="$V2"/>
											<xsl:with-param name="SN" select="$SN"/>
										</xsl:call-template>
									</xsl:when>
									<xsl:when test="$OT='BoolEventAttributeIsFalse'">
										<xsl:call-template name="EventSatisfied">
											<xsl:with-param name="Boolean" select="'False'"/>
											<xsl:with-param name="V1" select="$V1"/>
											<xsl:with-param name="V2" select="$V2"/>
											<xsl:with-param name="SN" select="$SN"/>
										</xsl:call-template>
									</xsl:when>
								</xsl:choose>
							</xsl:if>

							<xsl:value-of select="$newline"/>
							<xsl:value-of select="$newline"/>
							
							<!-- Optional Schedule Definition -->
							<!-- if has optional schedules definition -->
							<xsl:if test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-se:Schedules">
								<xsl:comment>
									<xsl:text>Definition of Schedule of </xsl:text>
									<xsl:value-of select="$name"/>
								</xsl:comment>
								<xsl:value-of select="$newline"/>
								<xsl:copy-of select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-se:Schedules"/>
								<xsl:value-of select="$newline"/>
								<xsl:value-of select="$newline"/>
							</xsl:if>

							<!-- GPA definition Constructs -->
							<xsl:comment>
								<xsl:text>Definition of Goal Policy Assertions</xsl:text>
							</xsl:comment>
							<xsl:value-of select="$newline"/>
							<xsl:element name="masc-gp:GoalPolicyAssertion">
								<xsl:attribute name="MASCID" select="$gp-MASCID"/>
								<!-- MASCID="GP-MASCID" -->
								<xsl:attribute name="ResponsibleParty" select="$responsibleParty"/>
								<xsl:attribute name="ManagementParty" select="$gp-managementParty"/>
								<xsl:element name="masc-se:WhenRef">
									<xsl:attribute name="To">
										<xsl:value-of select="$tns"/>
										<xsl:value-of select="$GPA_WF"/>
										<xsl:text>-</xsl:text>
										<xsl:value-of select="$GPA_WFMASCID"/>
									</xsl:attribute>
								</xsl:element>
								<xsl:element name="masc-ex:BooleanExpressionRef">
									<xsl:attribute name="To">
										<xsl:value-of select="$tns"/>
										<!-- BooleanExp MASCID varies for cases -->
										<!-- TODO: should be more easier after tns done -->
										<xsl:choose>
											<!-- case1. Boolean Constants  -->
											<xsl:when
												test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition[./True or ./False]">
												<xsl:text>BC-</xsl:text>
												<xsl:value-of select="$gp-MASCID"/>
											</xsl:when>
											<!-- case2 just 1 comparisions -->
											<xsl:when
												test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition[./Equal or ./NotEqual or ./Less or ./LessOrEqual or ./Greater or ./GreaterOrEqual]">
												<xsl:text>BE-</xsl:text>
												<xsl:value-of select="$gp-MASCID"/>
												<xsl:text>-</xsl:text>
												<xsl:value-of
												select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition//MonitoredData/@Name"/>
												<xsl:text>-</xsl:text>
												<xsl:value-of
												select="replace(generate-id($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition//MonitoredData), '([a-z]|[A-Z])', '')"
												/>
											</xsl:when>
											<!-- case3 Comparator with 2 comparisions -->
											<xsl:when
												test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition[./AND or ./OR]">
												<xsl:text>BE-</xsl:text>
												<xsl:value-of
												select="node-name($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition/*)"/>
												<xsl:text>-</xsl:text>
												<!-- generate id for ./OT (e.g. WhenFilteringCondition/AND) as sequence number -->
												<xsl:value-of
												select="replace(generate-id($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition/*), '([a-z]|[A-Z])', '')"
												/>
											</xsl:when>
											<!-- case4 This case would not appear in AdditionalInfo -->
											<xsl:when
												test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition[./BoolEventAttributeIsTrue or ./BoolEventAttributeIsFalse]">
												<xsl:text>BEV-</xsl:text>
												<xsl:value-of
												select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition/*/@EventRef"/>
												<xsl:text>-</xsl:text>
												<xsl:value-of
												select="replace(generate-id($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:GoalPA-AdditionalInfo[@MASCID=$gp-MASCID]/When-FilteringCondition/*), '([a-z]|[A-Z])', '')"
												/>
											</xsl:when>
										</xsl:choose>
									</xsl:attribute>
								</xsl:element>
							</xsl:element>
						</xsl:element>
					</xsl:when>
				</xsl:choose>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>

	<!-- TODO:tempates for booleanExpression cases (for When-FilteringCondition) -->
	<xsl:template match="masc-config:GoalPA-AdditionalInfo/When-FilteringCondition">
		<xsl:variable name="GPA_MASCID" select="../@MASCID"/>
		<!-- variables of Operator Type (e.g. AND, OR, True, False, Equal, NotEqual, etc) -->
		<xsl:variable name="OT" select="node-name(child::*)"/>

		<!-- case1. Boolean Constants
            e.g.
            <When-Filtering-Condition>
                <True/>
            </When-Filtering-Condition> 
        -->

		<xsl:if test="./True or ./False">
			<xsl:element name="masc-ex:BooleanExpression">
				<xsl:attribute name="MASCID">
					<xsl:text>GP-</xsl:text>
					<xsl:value-of select="$GPA_MASCID"/>
				</xsl:attribute>
				<xsl:element name="masc-ex:BooleanConstant">
					<xsl:attribute name="Type" select="lower-case(string($OT))"/>
				</xsl:element>
			</xsl:element>
		</xsl:if>

		<!-- case2 just 1 comparisions -->
		<xsl:if
			test="./Equal or ./NotEqual or ./Less or ./LessOrEqual or ./Greater or ./GreaterOrEqual">
			<xsl:choose>
				<!-- case2.1 with Operater in AwUExpression
                    e.g.
                    <Equal>
                    <Add>
                    <MonitoredData Name=""/>
                    <AwUConst Value="" Unit=""/>
                    </Add>
                    </Equal>   
                -->
				<xsl:when test="string(node-name(*/*))='Add' or string(node-name(*/*))='Multiply'">
					<xsl:variable name="operater" select="node-name(*/*)"/>
					<xsl:variable name="AwUExpUnit" select="*/*/AwUConst/@Unit"/>
					<xsl:call-template name="Comparision">
						<xsl:with-param name="GPA_MASCID" select="$GPA_MASCID"/>
						<xsl:with-param name="C" select="$OT"/>
						<xsl:with-param name="MDIN" select="*/*/MonitoredData/@Name"/>
						<xsl:with-param name="operater" select="$operater"/>
						<xsl:with-param name="AwUExpValue" select="*/*/AwUConst/@Value"/>
						<xsl:with-param name="SN_MonitoredData"
							select="replace(generate-id(*/*/MonitoredData), '([a-z]|[A-Z])', '')"/>
						<xsl:with-param name="SN_AwUConst"
							select="replace(generate-id(*/*/AwUConst), '([a-z]|[A-Z])', '')"/>
						<xsl:with-param name="OURI" select="substring-before($AwUExpUnit, ':')"/>
						<xsl:with-param name="T" select="substring-after($AwUExpUnit, ':')"/>
					</xsl:call-template>
				</xsl:when>
				<!-- case2.2 without Operater inside AwUExpression
                    e.g.
                    <Equal>
                    <MonitoredData Name=""/>
                    <AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>)
                    </Equal>   
                -->
				<xsl:otherwise>
					<xsl:variable name="AwUExpValue" select="*/AwUConst/@Value"/>
					<xsl:variable name="AwUExpUnit" select="*/AwUConst/@Unit"/>
					<xsl:call-template name="Comparision">
						<xsl:with-param name="GPA_MASCID" select="$GPA_MASCID"/>
						<xsl:with-param name="MDIN" select="*/MonitoredData/@Name"/>
						<xsl:with-param name="C" select="$OT"/>
						<xsl:with-param name="AwUExpValue" select="$AwUExpValue"/>
						<xsl:with-param name="AwUExpUnit" select="$AwUExpUnit"/>
						<xsl:with-param name="OURI" select="substring-before($AwUExpUnit, ':')"/>
						<xsl:with-param name="T" select="substring-after($AwUExpUnit, ':')"/>
						<!-- generate id for ./OT/C/MonitoredData (e.g. WhenFilteringCondition/Equal/MonitoredData) as sequence number -->
						<xsl:with-param name="SN_MonitoredData"
							select="replace(generate-id(*/MonitoredData), '([a-z]|[A-Z])', '')"/>
						<!-- generate id for ./OT/C/AwUConst (e.g. WhenFilteringCondition/Equal/AwUConst) as sequence number -->
						<xsl:with-param name="SN_AwUConst"
							select="replace(generate-id(*/AwUConst), '([a-z]|[A-Z])', '')"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>

		<!-- case3 Comparator with 2 comparisions -->
		<xsl:if test="./AND or ./OR">
			<xsl:element name="masc-ex:BooleanExpression">
				<xsl:attribute name="MASCID">
					<xsl:text>BE-</xsl:text>
					<xsl:value-of select="$OT"/>
					<xsl:text>-</xsl:text>
					<!-- generate id for ./OT (e.g. WhenFilteringCondition/AND) as sequence number -->
					<xsl:value-of select="replace(generate-id(child::*), '([a-z]|[A-Z])', '')"/>
				</xsl:attribute>
				<xsl:element name="masc-ex:BinaryBooleanOperator">
					<xsl:attribute name="Type" select="$OT"/>
				</xsl:element>
				<xsl:for-each select="*/child::*">
					<xsl:choose>
						<!-- case3.1 with Operater inside AwUExpression
                            e.g.
                            <When-Filtering-Condition>
                                <AND>
                                    <Equal>
                                        <Add>
                                            <MonitoredData Name=""/>
                                            <AwUConst Value="" Unit=""/>
                                        </Add>
                                    </Equal>
                                    <NotEqual>
                                        <Multiply>
                                            <MonitoredData Name=""/>
                                            <AwUConst Value=""/>
                                        <Multiply>
                                    </NotEqual>
                                </AND>
                            </When-Filtering-Condition>
                        -->
						<xsl:when test="./Add or ./Multiply">
							<xsl:variable name="AwUExpValue" select="*/AwUConst/@Value"/>
							<xsl:variable name="AwUExpUnit" select="*/AwUConst/@Unit"/>
							<xsl:variable name="operater" select="node-name(*)"/>
							<xsl:call-template name="Comparision">
								<xsl:with-param name="GPA_MASCID" select="$GPA_MASCID"/>
								<xsl:with-param name="MDIN" select="*/MonitoredData/@Name"/>
								<xsl:with-param name="C" select="local-name()"/>
								<xsl:with-param name="operater" select="$operater"/>
								<xsl:with-param name="AwUExpValue" select="$AwUExpValue"/>
								<xsl:with-param name="AwUExpUnit" select="$AwUExpUnit"/>
								<xsl:with-param name="OURI"
									select="substring-before($AwUExpUnit, ':')"/>
								<xsl:with-param name="T" select="substring-after($AwUExpUnit, ':')"/>
								<!-- generate id for ./OT/C/MonitoredData (e.g. WhenFilteringCondition/AND/Equal/MonitoredData) as sequence number -->
								<xsl:with-param name="SN_MonitoredData"
									select="replace(generate-id(*/MonitoredData), '([a-z]|[A-Z])', '')"/>
								<!-- generate id for ./OT/C/AwUConst (e.g. WhenFilteringCondition/AND/Equal/AwUConst) as sequence number -->
								<xsl:with-param name="SN_AwUConst"
									select="replace(generate-id(*/AwUConst), '([a-z]|[A-Z])', '')"/>
							</xsl:call-template>
						</xsl:when>
						<!-- case3.2 without Operater inside AwUExpression
                            e.g.
                            <When-Filtering-Condition>
                                <AND>
                                    <Equal>
                                        <MonitoredData Name=""/>
                                        <AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>
                                    </Equal>
                                    <NotEqual>
                                        <MonitoredData Name=""/>
                                        <AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>
                                    </NotEqual>
                                </AND>
                            </When-Filtering-Condition>
                        -->
						<xsl:otherwise>
							<xsl:variable name="AwUExpValue" select="./AwUConst/@Value"/>
							<xsl:variable name="AwUExpUnit" select="./AwUConst/@Unit"/>
							<xsl:call-template name="Comparision">
								<xsl:with-param name="GPA_MASCID" select="$GPA_MASCID"/>
								<xsl:with-param name="MDIN" select="./MonitoredData/@Name"/>
								<xsl:with-param name="C" select="local-name()"/>
								<xsl:with-param name="AwUExpValue" select="$AwUExpValue"/>
								<xsl:with-param name="AwUExpUnit" select="$AwUExpUnit"/>
								<xsl:with-param name="OURI"
									select="substring-before($AwUExpUnit, ':')"/>
								<xsl:with-param name="T" select="substring-after($AwUExpUnit, ':')"/>
								<!-- generate id for ./OT/C/MonitoredData (e.g. WhenFilteringCondition/AND/Equal/MonitoredData) as sequence number -->
								<xsl:with-param name="SN_MonitoredData"
									select="replace(generate-id(./MonitoredData), '([a-z]|[A-Z])', '')"/>
								<!-- generate id for ./OT/C/AwUConst (e.g. WhenFilteringCondition/AND/Equal/AwUConst) as sequence number -->
								<xsl:with-param name="SN_AwUConst"
									select="replace(generate-id(./AwUConst), '([a-z]|[A-Z])', '')"/>
							</xsl:call-template>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:for-each>
			</xsl:element>
		</xsl:if>

		<!-- case4 This case would not appear in AdditionalInfo -->
		<xsl:if test="./BoolEventAttributeIsTrue or ./BoolEventAttributeIsFalse">
			<xsl:variable name="V1" select="*/@EventRef"/>
			<xsl:variable name="V2" select="*/@Attribute"/>
			<xsl:variable name="SN" select="replace(generate-id(*), '([a-z]|[A-Z])', '')"/>
			<xsl:choose>
				<xsl:when test="./BoolEventAttributeIsTrue">
					<xsl:call-template name="EventSatisfied">
						<xsl:with-param name="Boolean" select="'True'"/>
						<xsl:with-param name="V1" select="$V1"/>
						<xsl:with-param name="V2" select="$V2"/>
						<xsl:with-param name="SN" select="$SN"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:when test="./BoolEventAttributeIsFalse">
					<xsl:call-template name="EventSatisfied">
						<xsl:with-param name="Boolean" select="'False'"/>
						<xsl:with-param name="V1" select="$V1"/>
						<xsl:with-param name="V2" select="$V2"/>
						<xsl:with-param name="SN" select="$SN"/>
					</xsl:call-template>
				</xsl:when>
			</xsl:choose>
		</xsl:if>
	</xsl:template>

	<!-- Template for comparision -->
	<xsl:template name="Comparision">
		<xsl:param name="GPA_MASCID"/>
		<xsl:param name="MDIN"/>
		<xsl:param name="C"/>
		<xsl:param name="AwUExpValue"/>
		<xsl:param name="AwUExpUnit"/>
		<xsl:param name="OURI"/>
		<xsl:param name="T"/>
		<xsl:param name="SN_MonitoredData"/>
		<xsl:param name="SN_AwUConst"/>
		<xsl:param name="operater"/>

		<xsl:element name="masc-ex:BooleanExpression">
			<xsl:attribute name="MASCID">
				<xsl:text>BE-</xsl:text>
				<xsl:value-of select="$GPA_MASCID"/>
				<xsl:text>-</xsl:text>
				<xsl:value-of select="$MDIN"/>
				<xsl:text>-</xsl:text>
				<xsl:value-of select="$SN_MonitoredData"/>
			</xsl:attribute>
			<xsl:element name="masc-ex:ArithmeticWithUnitComparator">
				<xsl:attribute name="Type">
					<xsl:choose>
						<xsl:when test="lower-case(string($C))='equal'">
							<xsl:text>==</xsl:text>
						</xsl:when>
						<xsl:when test="lower-case(string($C))='notequal'">
							<xsl:text>!=</xsl:text>
						</xsl:when>
						<xsl:when test="lower-case(string($C))='less'">
							<xsl:text disable-output-escaping="yes">&lt;</xsl:text>
						</xsl:when>
						<xsl:when test="lower-case(string($C))='greater'">
							<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
						</xsl:when>
						<xsl:when test="lower-case(string($C))='lessorequal'">
							<xsl:text disable-output-escaping="yes">&lt;=</xsl:text>
						</xsl:when>
						<xsl:when test="lower-case(string($C))='greaterorequal'">
							<xsl:text disable-output-escaping="yes">&gt;=</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$C"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
			</xsl:element>
			<xsl:element name="masc-ex:ArithmeticWithUnitExpression">
				<xsl:attribute name="MASCID">
					<xsl:text>AE-</xsl:text>
					<xsl:value-of select="$GPA_MASCID"/>
					<xsl:text>-</xsl:text>
					<xsl:value-of select="$MDIN"/>
					<xsl:text>-</xsl:text>
					<xsl:value-of select="$SN_AwUConst"/>
				</xsl:attribute>
				<xsl:element name="masc-gp:MonitoredDataItemRef">
					<xsl:attribute name="To">
						<xsl:value-of select="$tns"/>
						<xsl:value-of select="$MDIN"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>
			<xsl:choose>
				<!-- sub-case1. Arithmetic Expression with operater -->
				<xsl:when test="string($operater)='Add' or string($operater)='Multiply'">
					<xsl:call-template name="AWU_with_Operator">
						<xsl:with-param name="GPA_MASCID" select="$GPA_MASCID"/>
						<xsl:with-param name="MDIN" select="$MDIN"/>
						<xsl:with-param name="O" select="$operater"/>
						<xsl:with-param name="AV" select="$AwUExpValue"/>
						<xsl:with-param name="OURI" select="$OURI"/>
						<xsl:with-param name="T" select="$T"/>
						<xsl:with-param name="SN" select="$SN_AwUConst"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<!-- subCase2. Arithmetic Expression With Unit and without operater -->
					<xsl:if test="$AwUExpUnit">
						<xsl:call-template name="Arithmetic_With_Unit">
							<xsl:with-param name="GPA_MASCID" select="$GPA_MASCID"/>
							<xsl:with-param name="MDIN" select="$MDIN"/>
							<xsl:with-param name="SN" select="$SN_AwUConst"/>
							<xsl:with-param name="AV" select="$AwUExpValue"/>
							<xsl:with-param name="OURI" select="$OURI"/>
							<xsl:with-param name="T" select="$T"/>
						</xsl:call-template>
					</xsl:if>
					<!-- subCase3. if ArithmeticExpression has no Unit and without operater -->
					<xsl:if test="not($AwUExpUnit)">
						<xsl:call-template name="Arithmetic_Constants_Without_Unit">
							<xsl:with-param name="GPA_MASCID" select="$GPA_MASCID"/>
							<xsl:with-param name="MDIN" select="$MDIN"/>
							<xsl:with-param name="SN" select="$SN_AwUConst"/>
							<xsl:with-param name="CV" select="$AwUExpValue"/>
						</xsl:call-template>
					</xsl:if>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>

	<!-- template for Arithmetic-with-unit constants -->
	<xsl:template name="Arithmetic_With_Unit">
		<xsl:param name="GPA_MASCID"/>
		<xsl:param name="MDIN"/>
		<xsl:param name="SN"/>
		<xsl:param name="AV"/>
		<xsl:param name="OURI"/>
		<xsl:param name="T"/>
		<xsl:element name="masc-ex:ArithmeticWithUnitExpression">
			<xsl:attribute name="MASCID">
				<xsl:text>AUE-</xsl:text>
				<xsl:value-of select="$GPA_MASCID"/>
				<xsl:text>-</xsl:text>
				<xsl:value-of select="$MDIN"/>
				<xsl:text>-</xsl:text>
				<xsl:value-of select="$SN"/>
			</xsl:attribute>
			<xsl:element name="masc-ex:ArithmeticWithUnitConstant">
				<xsl:element name="masc-ex:ArithmeticValue">
					<xsl:attribute name="Value" select="$AV"/>
				</xsl:element>
				<xsl:element name="masc-ex:Unit">
					<xsl:attribute name="Ontology" select="$OURI"/>
					<xsl:attribute name="Type" select="$T"/>
				</xsl:element>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<!-- template for arithmetic-with-unit -->
	<xsl:template name="Arithmetic_Constants_Without_Unit">
		<xsl:param name="GPA_MASCID"/>
		<xsl:param name="MDIN"/>
		<xsl:param name="SN"/>
		<xsl:param name="CV"/>
		<xsl:element name="masc-ex:ArithmeticExpression">
			<xsl:attribute name="MASCID">
				<xsl:text>AC-</xsl:text>
				<xsl:value-of select="$GPA_MASCID"/>
				<xsl:text>-</xsl:text>
				<xsl:value-of select="$MDIN"/>
				<xsl:text>-</xsl:text>
				<xsl:value-of select="$SN"/>
			</xsl:attribute>
			<xsl:element name="masc-ex:FloatConstant">
				<xsl:attribute name="Value" select="$CV"/>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<!-- template for arithmetic-with-unit operators -->
	<xsl:template name="AWU_with_Operator">
		<xsl:param name="GPA_MASCID"/>
		<xsl:param name="MDIN"/>
		<xsl:param name="SN"/>
		<xsl:param name="O"/>
		<xsl:param name="AV"/>
		<xsl:param name="OURI"/>
		<xsl:param name="T"/>
		<xsl:element name="masc-ex:ArithmeticWithUnitExpression">
			<xsl:attribute name="MASCID">
				<xsl:text>AUE-</xsl:text>
				<xsl:value-of select="$GPA_MASCID"/>
				<xsl:text>-</xsl:text>
				<xsl:value-of select="$SN"/>
			</xsl:attribute>
			<xsl:element name="masc-ex:BinaryArithmeticWithUnitOperator">
				<xsl:attribute name="Type">
					<xsl:choose>
						<xsl:when test="lower-case(string($O))='add'">
							<xsl:text>+</xsl:text>
						</xsl:when>
						<xsl:when test="lower-case(string($O))='multiply'">
							<xsl:text>*</xsl:text>
						</xsl:when>
					</xsl:choose>
				</xsl:attribute>
			</xsl:element>
			<xsl:element name="masc-ex:ArithmeticWithUnitExpression">
				<xsl:attribute name="MASCID">
					<xsl:text>AUE-</xsl:text>
					<xsl:value-of select="$GPA_MASCID"/>
					<xsl:text>-</xsl:text>
					<xsl:value-of select="$MDIN"/>
					<xsl:text>-</xsl:text>
					<xsl:value-of select="$SN"/>
				</xsl:attribute>
				<xsl:element name="masc-gp:MonitoredDataItemRef">
					<xsl:attribute name="To">
						<xsl:value-of select="$tns"/>
						<xsl:value-of select="$MDIN"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>
			<xsl:choose>
				<!-- sub-case1. for Add Operater, the Arithmetic Expression must has Unit -->
				<xsl:when test="string($O)='Add'">
					<xsl:call-template name="Arithmetic_With_Unit">
						<xsl:with-param name="GPA_MASCID" select="$GPA_MASCID"/>
						<xsl:with-param name="MDIN" select="$MDIN"/>
						<xsl:with-param name="SN" select="$SN"/>
						<xsl:with-param name="AV" select="$AV"/>
						<xsl:with-param name="OURI" select="$OURI"/>
						<xsl:with-param name="T" select="$T"/>
					</xsl:call-template>
				</xsl:when>
				<!-- sub-case2.for Multiply Operater, the Arithmetic Expression must has NO Unit -->
				<xsl:when test="string($O)='Multiply'">
					<xsl:call-template name="Arithmetic_Constants_Without_Unit">
						<xsl:with-param name="SN" select="$SN"/>
						<xsl:with-param name="MDIN" select="$MDIN"/>
						<xsl:with-param name="GPA_MASCID" select="$GPA_MASCID"/>
						<xsl:with-param name="CV" select="$AV"/>
					</xsl:call-template>
				</xsl:when>
			</xsl:choose>
		</xsl:element>
	</xsl:template>

	<!-- template for Boolean Event Attribute Satisfied -->
	<xsl:template name="EventSatisfied">
		<xsl:param name="Boolean"/>
		<xsl:param name="V1"/>
		<xsl:param name="V2"/>
		<xsl:param name="SN"/>
		<xsl:element name="masc-ex:BooleanExpression">
			<xsl:attribute name="MASCID">
				<xsl:text>BEV-</xsl:text>
				<xsl:value-of select="$V1"/>
				<xsl:text>-</xsl:text>
				<xsl:value-of select="$SN"/>
			</xsl:attribute>
			<xsl:element name="masc-ex:ArithmeticWithUnitComparator">
				<xsl:attribute name="Type" select="'=='"/>
			</xsl:element>
			<xsl:element name="masc-ex:BooleanExpression">
				<xsl:attribute name="MASCID">
					<xsl:text>BEV-</xsl:text>
					<xsl:value-of select="$Boolean"/>
					<xsl:text>-</xsl:text>
					<xsl:value-of select="$SN"/>
				</xsl:attribute>
				<xsl:element name="masc-gp:BooleanVariable">
					<xsl:attribute name="AWUName">
						<xsl:value-of select="$tns"/>
						<xsl:value-of select="$V1"/>
						<xsl:text>.</xsl:text>
						<xsl:value-of select="$V2"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<!-- template for select last occurrence of a substring, used if there're 2 comparisions inside BooleanExpression TaggedValue -->
	<xsl:template name="substring-after-last">
		<xsl:param name="string"/>
		<xsl:param name="delimiter"/>
		<xsl:choose>
			<xsl:when test="contains($string, $delimiter)">
				<xsl:call-template name="substring-after-last">
					<xsl:with-param name="string" select="substring-after($string, $delimiter)"/>
					<xsl:with-param name="delimiter" select="$delimiter"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$string"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="substring-before-last">
		<!-- ab-cd-ef -->
		<xsl:param name="string"/>
		<xsl:param name="delimiter"/>
		<xsl:choose>
			<xsl:when test="$delimiter and contains($string, $delimiter)">
				<xsl:variable name="temp" select="substring-after($string, $delimiter)"/>
				<!-- cd-ef -->
				<xsl:value-of select="substring-before($string, $delimiter)"/>
				<!-- ab -->
				<xsl:choose>
					<xsl:when test="contains($temp, $delimiter)">
						<xsl:value-of select="$delimiter"/>
						<xsl:call-template name="substring-before-last">
							<xsl:with-param name="string" select="$temp"/>
							<xsl:with-param name="delimiter" select="$delimiter"/>
						</xsl:call-template>
					</xsl:when>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$string"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


</xsl:stylesheet>
